<?php
class Configurations_Model extends MY_Model
{
    function __construct()
    {
        parent::__construct();
        //table name khac class model
        $this->table_name = 'configuration';
    }

    public function get_configuration($options = array())
    {
        if (isset($options['lang'])) {
            $this->db->where('configuration.lang', $options['lang']);
        } else {
            $this->db->where('configuration.lang', DEFAULT_LANGUAGE);
        }

        $result = $this->db->get('configuration');
        if($result->num_rows() > 0){
            if(isset($options['array']))
                return $result->row_array();
            return $result->row(0);
        }else{
            return NULL;
        }
    }
    
    public function delete_logo($lang = DEFAULT_LANGUAGE, $image_path = UPLOAD_PATH_LOGO)
    {
        $image = $this->get_configuration(array('lang' => $lang));
        if (is_object($image))
        {
            if (file_exists($image_path . $image->logo))
                @unlink($image_path . $image->logo);
        }
        return NULL;
    }
    
    public function upload_logo($image_path = UPLOAD_PATH_LOGO,$lang = DEFAULT_LANGUAGE)
    {
        $this->load->library('upload');
        
        $config['upload_path'] = UPLOAD_PATH_LOGO;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1024';
        $config['encrypt_name'] = FALSE;
        $image_file_name        = dechex((int)date('His'));

        $this->upload->initialize($config);
        
        if ( ! $this->upload->do_upload('userfile') )
        {
            $error = $this->upload->display_errors();
            $msg= $this->_last_message = $error;
            // neu co loi return false, thong bao loi upload anh
            return FALSE;
        }
        else 
        {
            $image = $this->upload->data();
            //delete image truoc do neu edit
            if(!empty($image)){
                $this->delete_logo($lang,$image_path);
            }
            
            $image_dimension = $image['image_width'].'x'.$image['image_height'];
            
            $image_file_name = url_title($image['raw_name']).'_'.$image_file_name.$image['file_ext'];
            
//            $image_file_name .= $image['file_ext'];

            $this->load->library('image_lib');
            
              // Start processing the uploaded image
            $config['image_library'] = 'gd2';
            $config['source_image'] = UPLOAD_PATH_LOGO . $image['file_name'];
            $config['maintain_ratio'] = TRUE;
            
            // 1. Create the main image
            $config['new_image'] = $image_path . $image_file_name;
            
            $this->image_lib->initialize($config);

            $this->image_lib->resize();

            $this->image_lib->clear();

            if (file_exists(UPLOAD_PATH_LOGO . $image['file_name']))
                    unlink(UPLOAD_PATH_LOGO . $image['file_name']);
            
            $data = array(
                'image_name'    => $image_file_name,
                'image_dimension' => $image_dimension,
            );
            
            return $data;
        }
    }
    
    public function delete_favicon($lang = DEFAULT_LANGUAGE, $image_path = UPLOAD_PATH_FAVICON)
    {
        $image = $this->get_configuration(array('lang' => $lang));
        if (is_object($image))
        {
            if (file_exists($image_path . $image->favicon))
                @unlink($image_path . $image->favicon);
        }
        return NULL;
    }
    
    public function upload_favicon($image_path = UPLOAD_PATH_FAVICON,$lang = DEFAULT_LANGUAGE)
    {
        $this->load->library('upload');
        
        $config['upload_path'] = UPLOAD_PATH_FAVICON;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1024';
        $config['encrypt_name'] = FALSE;
        $image_file_name        = dechex((int)date('His'));
        
        $this->upload->initialize($config);
        
        if ( ! $this->upload->do_upload('userfile2') )
        {
            $error = $this->upload->display_errors();
            $msg= $this->_last_message = $error;
            // neu co loi return false, thong bao loi upload anh
            return FALSE;
        }
        else 
        {
            $image = $this->upload->data();
            
            //delete image truoc do neu edit
            if(!empty($image)){
                $this->delete_favicon($lang,$image_path);
            }
            
            $image_dimension = $image['image_width'].'x'.$image['image_height'];
            
            $image_file_name = url_title($image['raw_name']).'_'.$image_file_name.$image['file_ext'];

            $this->load->library('image_lib');
            
              // Start processing the uploaded image
            $config['image_library'] = 'gd2';
            $config['source_image'] = UPLOAD_PATH_FAVICON . $image['file_name'];
            $config['maintain_ratio'] = TRUE;
            
            // 1. Create the main image
            $config['new_image'] = $image_path . $image_file_name;
            
            $this->image_lib->initialize($config);

            $this->image_lib->resize();

            $this->image_lib->clear();

            if (file_exists(UPLOAD_PATH_FAVICON . $image['file_name']))
                    unlink(UPLOAD_PATH_FAVICON . $image['file_name']);
            
            $data = array(
                'image_name'    => $image_file_name,
                'image_dimension' => $image_dimension,
            );

            return $data;
        }
    }
}
?>